package com.brillilab.service.core.kit.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.brillilab.common.alioss.OssFileEnum;
import com.brillilab.common.entity.PageVo;
import com.brillilab.dao.mapper.kit.ReagentPlasmidFragmentMapper;
import com.brillilab.dao.mapper.kit.ReagentPlasmidMapper;
import com.brillilab.dao.mapper.kit.ReagentPlasmidUseMapper;
import com.brillilab.domain.pa.kit.ReagentPlasmidTemplatePa;
import com.brillilab.domain.po.kit.ReagentPlasmid;
import com.brillilab.domain.po.kit.ReagentPlasmidFragment;
import com.brillilab.domain.po.kit.ReagentPlasmidUse;
import com.brillilab.domain.po.system.FileManage;
import com.brillilab.domain.vo.kit.PlasmidTemplateListVo;
import com.brillilab.service.core.kit.IReagentPlasmidService;
import com.brillilab.service.core.system.IFileManageService;
import com.github.pagehelper.PageHelper;
@Service
@Transactional
public class ReagentPlasmidServiceImpl implements IReagentPlasmidService{
	
	@Autowired
	private ReagentPlasmidMapper reagentPlasmidMapper;	
	@Autowired
	private ReagentPlasmidFragmentMapper reagentPlasmidFragmentMapper;	
	@Autowired
	private ReagentPlasmidUseMapper reagentPlasmidUseMapper;	
	@Autowired
	private IFileManageService fileManageService;
	
	@Override
	public ReagentPlasmid insert(ReagentPlasmid entity) {
		Integer insert = reagentPlasmidMapper.insert(entity);
		Assert.isTrue(insert==1, "添加失败");
		return entity;
	}
	
	@Override
	public boolean deleteById(Long id) {
		Integer b = reagentPlasmidMapper.deleteById(id);
		return b==1 ? true: false;
	}
	
	@Override
	public boolean updateById(ReagentPlasmid entity) {
		entity.setUpdateTime(new Date());
		Integer b = reagentPlasmidMapper.updateById(entity);
		return b==1 ? true: false;
	}
	
	@Override
	public ReagentPlasmid selectById(Long id) {
		return reagentPlasmidMapper.selectById(id);
	}

	@Override
	public PageVo<PlasmidTemplateListVo> selectTemplatePage(ReagentPlasmidTemplatePa pa) {
		if(pa.getType()==3){
			//模板库最近使用
			List<PlasmidTemplateListVo> list2 = this.selectPlasmidUseList(pa.getLabId(), 1);
			//实验室质粒库最近使用
			List<PlasmidTemplateListVo> list = this.selectPlasmidUseList(pa.getLabId(), 2);
			list.addAll(list2);
			return new PageVo<PlasmidTemplateListVo>(1,10,10,list);
		}else{
			PageHelper.startPage(pa.getPageNum(), pa.getPageSize());
			List<PlasmidTemplateListVo> list =reagentPlasmidMapper.selectPlasmidTemplateList(pa.getLabId(),pa.getPlasmidName(),pa.getType());
			return new PageVo<PlasmidTemplateListVo>(list);
		}
		
	}
	
	@SuppressWarnings("unchecked")
	public List<PlasmidTemplateListVo> selectPlasmidUseList(Long labId,Integer type) {
		List<PlasmidTemplateListVo> list = new ArrayList<>();
		LambdaQueryWrapper<ReagentPlasmidUse> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(ReagentPlasmidUse::getLabId,labId);
		wrapper.eq(ReagentPlasmidUse::getType,type);
		wrapper.orderByDesc(ReagentPlasmidUse::getUseTime);
		IPage<ReagentPlasmidUse> iPage = new Page<>(1,5);
		IPage<ReagentPlasmidUse> page = reagentPlasmidUseMapper.selectPage(iPage, wrapper);
		if(page!=null && page.getRecords()!=null){
			for (ReagentPlasmidUse use : page.getRecords()) {
				PlasmidTemplateListVo vo = new PlasmidTemplateListVo();
				vo.setLabId(use.getLabId());
				vo.setPlasmidTemplateId(use.getReagentPlasmidId());
				vo.setPlasmidName(use.getReagentPlasmidName());
				vo.setType(type);
				list.add(vo);
			}
			
		}
		return list;
	}

	@Override
	public ReagentPlasmid selectInfo(Long id) {
		ReagentPlasmid plasmid = this.selectById(id);
		if(plasmid!=null) {
			if(plasmid.getLabId()==null) {
				List<FileManage> fileList1 = fileManageService.getFileList(plasmid.getAbstractFile());
				plasmid.setAbstractFileList(fileList1);
				List<FileManage> fileList2 = fileManageService.getFileList(plasmid.getAtlasFile());
				plasmid.setAtlasFileList(fileList2);
				List<FileManage> fileList3 = fileManageService.getFileList(plasmid.getReportFile());
				plasmid.setReportFileList(fileList3);
				List<FileManage> fileList4 = fileManageService.getFileList(plasmid.getOtherFile());
				plasmid.setOtherFileList(fileList4);
			}else {
				List<FileManage> fileList1 = fileManageService.selectList(OssFileEnum.PlasmidAbstractFile, plasmid.getId());
				plasmid.setAbstractFileList(fileList1);
				List<FileManage> fileList2 = fileManageService.selectList(OssFileEnum.PlasmidAtlasFile, plasmid.getId());
				plasmid.setAtlasFileList(fileList2);
				List<FileManage> fileList3 = fileManageService.selectList(OssFileEnum.PlasmidReportFile, plasmid.getId());
				plasmid.setReportFileList(fileList3);
				List<FileManage> fileList4 = fileManageService.selectList(OssFileEnum.PlasmidOtherFile, plasmid.getId());
				plasmid.setOtherFileList(fileList4);
			}
		}
		return plasmid;
	}

	@Override
	public ReagentPlasmid selectOne(Long kitId, Long reagentId) {
		ReagentPlasmid reagentPlasmid = null;
		if(kitId!=null && reagentId!=null){
			LambdaQueryWrapper<ReagentPlasmid> wrapper = new LambdaQueryWrapper<>();
			wrapper.eq(ReagentPlasmid::getKitId,kitId);
			wrapper.eq(ReagentPlasmid::getReagentId,reagentId);
			reagentPlasmid = reagentPlasmidMapper.selectOne(wrapper);
			if(reagentPlasmid!=null){
				List<FileManage> fileList1 = fileManageService.selectList(OssFileEnum.PlasmidAbstractFile, reagentPlasmid.getId());
				reagentPlasmid.setAbstractFileList(fileList1);
				List<FileManage> fileList2 = fileManageService.selectList(OssFileEnum.PlasmidAtlasFile, reagentPlasmid.getId());
				reagentPlasmid.setAtlasFileList(fileList2);
				List<FileManage> fileList3 = fileManageService.selectList(OssFileEnum.PlasmidReportFile, reagentPlasmid.getId());
				reagentPlasmid.setReportFileList(fileList3);
				List<FileManage> fileList4 = fileManageService.selectList(OssFileEnum.PlasmidOtherFile, reagentPlasmid.getId());
				reagentPlasmid.setOtherFileList(fileList4);

				LambdaQueryWrapper<ReagentPlasmidFragment> attrWrapper = new LambdaQueryWrapper<>();
				attrWrapper.eq(ReagentPlasmidFragment::getKitId,kitId);
				attrWrapper.eq(ReagentPlasmidFragment::getReagentId,reagentId);
				List<ReagentPlasmidFragment> fragmentList = reagentPlasmidFragmentMapper.selectList(attrWrapper);
				reagentPlasmid.setFragmentList(fragmentList);
			}
		}
		return reagentPlasmid;
	}

	@Override
	public ReagentPlasmidUse insertUse(ReagentPlasmidUse entity) {
		Integer insert = reagentPlasmidUseMapper.insert(entity);
		Assert.isTrue(insert==1, "添加失败");
		return entity;
	}

	@Override
	public ReagentPlasmidUse selectUseOne(Long labId, Long reagentPlasmidId) {
		LambdaQueryWrapper<ReagentPlasmidUse> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(ReagentPlasmidUse::getLabId,labId);
		wrapper.eq(ReagentPlasmidUse::getReagentPlasmidId,reagentPlasmidId);
		return reagentPlasmidUseMapper.selectOne(wrapper);
	}

	@Override
	public boolean updateUseById(ReagentPlasmidUse entity) {
		Integer b = reagentPlasmidUseMapper.updateById(entity);
		return b==1 ? true: false;
	}
}
